<?php

namespace addons\Minerva\api\modules\v1\controllers;

use Yii;
use addons\Minerva\common\helpers\UploadHelper;
use addons\Minerva\common\models\MainConfig;
use addons\Minerva\common\traits\FileActions;
use api\controllers\OnAuthController;
use common\helpers\ResultHelper;
use common\models\common\Attachment;

/**
 * 资源上传控制器
 *
 * Class FileController
 * @Package addons\Minerva\api\modules\v1\controllers
 * @Author 小武哥 <1240035958@qq.com>
 */
class FileController extends OnAuthController
{
    use FileActions;

    /**
     * @var string
     */
    public $modelClass = '';

//    protected $authOptional = ['images','videos','voices','files','merge','verify-md5'];

    public function actionFiles()
    {
        $path = $this->getPath();
        $upload = new UploadHelper(Yii::$app->request->post(), Attachment::UPLOAD_TYPE_FILES);
        $upload->verifyFile();
        return $upload->save($path);
        return $upload->getBaseInfo(Yii::$app->request->post('cate_id',0));
    }

    private function getPath(){
        $config = MainConfig::find()
            ->asArray()
            ->one();
        if(!$config){
            return ResultHelper::json(422, '请先配置基本参数');
        }
        $path = '';
        if($config['path_dep1'] == 1) $path = $path . $config['path_dep1'];
        if($config['path_dep2'] == 1) $path = $path . '/' . $config['path_dep2'];
        if($config['path_uid'] == 1){
            $member_id = Yii::$app->user->identity->member_id;
            $member = \common\models\backend\Member::find()
                ->where(['id' => $member_id])
                ->one();
            $path = $path . '/' . $member['username'];
        }
        if($config['path_task'] == 1) $path = $path. '/' . Yii::$app->request->get('task_title');
        if($config['path_time'] == 1) $path = $path. '/' . date('Y/m/d', time()) . '/';
        return $path;
    }
}